<?xml version='1.0' encoding='iso-8859-1'?>
<!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
	<head>
		<title>
			hexload.c
			</title>
		<meta http-equiv='content-type' content='text/html;iso-8859-1'/>
		<meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
		<meta name='author' content='cmaier@cmassoc.net'/>
		<meta name='robots' content='noindex,nofollow'/>
		<link href='toolkit.css' rel='stylesheet' type='text/css'/>
		</head>
	<body>
		<div class='headerlink'>
			[<a href='hexencode.c.html' title=' hexencode.c '>PREV</a>]
			[<a href='toolkit.html' title=' Index '>HOME</a>]
			[<a href='hexoffset.c.html' title=' hexoffset.c '>NEXT</a>]
			</div>
<pre>
/*====================================================================*
 *
 *   size_t hexload (void * memory, size_t extent, FILE * fp);
 *
 *   memory.h
 *
 *   read a file and convert hexadecimal octets to binary bytes then
 *   store them in consecutive memory locations up to a given length;
 *   return the actual number of bytes stored;
 *
 *   digits may be consecutive or separated by white space or comment
 *   text; a colon terminates a frame, to allow multiple frames in a
 *   on one file;
 *
 *   Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
 *   Copyright (c) 2001-2006 by Charles Maier Associates;
 *   Licensed under the Internet Software Consortium License;
 *
 *--------------------------------------------------------------------*/

#ifndef HEXLOAD_SOURCE
#define HEXLOAD_SOURCE

#include &lt;stdio.h&gt;
#include &lt;ctype.h&gt;
#include &lt;stdint.h&gt;
#include &lt;errno.h&gt;

#include &quot;../tools/memory.h&quot;
#include &quot;../tools/error.h&quot;
#include &quot;../tools/chars.h&quot;

/*====================================================================*
 *   private variables;
 *--------------------------------------------------------------------*/

static unsigned row = 1;
static unsigned col = 1;

/*====================================================================*
 *
 *   signed fpgetc (FILE * fp)
 *
 *   return the next input character after updating the file cursor
 *   position;
 *
 *   Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
 *   Copyright (c) 2001-2006 by Charles Maier Associates;
 *   Licensed under the Internet Software Consortium License;
 *
 *--------------------------------------------------------------------*/

static signed fpgetc (FILE * fp)

{
	extern unsigned row;
	extern unsigned col;
	signed c = getc (fp);
	if (c == '\n')
	{
		row++;
		col = 0;
	}
	else
	{
		col++;
	}
	return (c);
}

/*====================================================================*
 *
 *   size_t hexload (void * memory, size_t extent, FILE * fp);
 *
 *   memory.h
 *
 *   read a file and convert hexadecimal octets to binary bytes then
 *   store them in consecutive memory locations up to a given length;
 *   return the actual number of bytes stored;
 *
 *   digits may be consecutive or separated by white space or comment
 *   text; a colon terminates a frame, to allow multiple frames in a
 *   on one file;
 *
 *   Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
 *   Copyright (c) 2001-2006 by Charles Maier Associates;
 *   Licensed under the Internet Software Consortium License;
 *
 *--------------------------------------------------------------------*/

size_t hexload (void * memory, size_t extent, FILE * fp)

{
	extern unsigned row;
	extern unsigned col;
	byte * origin = (uint8_t *)(memory);
	byte * offset = (uint8_t *)(memory);
	unsigned digits = sizeof (* offset) &lt;&lt; 1;
	unsigned digit = 0;
	signed c = EOF;
	while ((extent) &amp;&amp; ((c = fpgetc (fp)) != EOF) &amp;&amp; (c != ';'))
	{
		if (isspace (c))
		{
			continue;
		}
		if (c == '#')
		{
			do
			{
				c = fpgetc (fp);
			}
			while (nobreak (c));
			continue;
		}
		if (c == '/')
		{
			c = fpgetc (fp);
			if (c == '/')
			{
				do
				{
					c = fpgetc (fp);
				}
				while (nobreak (c));
				continue;
			}
			if (c == '*')
			{
				while ((c != '/') &amp;&amp; (c != EOF))
				{
					while ((c != '*') &amp;&amp; (c != EOF))
					{
						c = fpgetc (fp);
					}
					c = fpgetc (fp);
				}
				continue;
			}
			continue;
		}
		if ((c &gt;= '0') &amp;&amp; (c &lt;= '9'))
		{
			*offset *= 16;
			*offset += c - '0';
			if (!(++digit % digits))
			{
				offset++;
				extent--;
			}
			continue;
		}
		if ((c &gt;= 'A') &amp;&amp; (c &lt;= 'F'))
		{
			*offset *= 16;
			*offset += 10;
			*offset += c - 'A';
			if (!(++digit % digits))
			{
				offset++;
				extent--;
			}
			continue;
		}
		if ((c &gt;= 'a') &amp;&amp; (c &lt;= 'f'))
		{
			*offset *= 16;
			*offset += 10;
			*offset += c - 'a';
			if (!(++digit % digits))
			{
				offset++;
				extent--;
			}
			continue;
		}

#if 1

		error (1, ENOTSUP, &quot;Unexpected character '%c': row %d: col %d&quot;, c, row, col);

#else

		return ((size_t)(-1));

#endif

	}
	if (digit &amp; 1)
	{

#if 1

		error (1, ENOTSUP, &quot;Odd digit count (%d) in source&quot;, digit);

#else

		return ((size_t)(-1));

#endif

	}
	return (offset - origin);
}

#endif


</pre>
		<div class='footerlink'>
			[<a href='hexencode.c.html' title=' hexencode.c '>PREV</a>]
			[<a href='toolkit.html' title=' Index '>HOME</a>]
			[<a href='hexoffset.c.html' title=' hexoffset.c '>NEXT</a>]
			</div>
		</body>
	</html>
